<?php if (! defined('BASEPATH')) exit('No direct script access allowed');
class Image_model extends CI_Model{

	function __construct()
    {
        parent::__construct();
	}
	
	function getAllFile($num,$offset,$arr_search=array())
	{
        $this->db->select('sys_image_data.*,sys_image_category.image_category_name');
        $this->db->join('sys_image_category','sys_image_data.image_category_id = sys_image_category.image_category_id','LEFT');
        /*Begin search*/
        if($arr_search){
            switch($arr_search['field_search'])
            {
                case 'TT':
                    if($arr_search['key_search']!='')
                        $this->db->like('lb_title',$arr_search['key_search']);
                    break ;
                case 'DEC':
                    if($arr_search['key_search']!='')
                        $this->db->like('description',$arr_search['key_search']);
                    break ;
                case 'EXT':
                    if($arr_search['key_search']!='')
                        $this->db->like('lb_ext',$arr_search['key_search']);
                    break ;
                
            }
            if($arr_search['image_category_id']!=''){
                    $this->db->where('sys_image_data.image_category_id',$arr_search['image_category_id']);
                }
        }
		/*End search*/
         $ar_img = array('gif','png','jpg','jpeg');
         $this->db->where_in('lb_ext',$ar_img);
		 $this->db->order_by('image_data_id','DESC');
		 $query = $this->db->get('sys_image_data',$num,$offset);
		 return $query->result();
	}
	
	function getNumFile($arr_search=array())
	{
        $this->db->select('sys_image_data.*,sys_image_category.image_category_name');
        $this->db->join('sys_image_category','sys_image_data.image_category_id = sys_image_category.image_category_id','LEFT');
        /*Begin search*/
        if($arr_search){
            switch($arr_search['field_search'])
            {
                case 'TT':
                    $this->db->like('lb_title',$arr_search['key_search']);
                    break ;
                case 'DEC':
                    $this->db->like('description',$arr_search['key_search']);
                    break ;
                case 'EXT':
                    $this->db->like('lb_ext',$arr_search['key_search']);
                    break ;
            }
            if($arr_search['image_category_id']!=''){
                $this->db->where('sys_image_data.image_category_id',$arr_search['image_category_id']);
            }
        }
		/*End search*/
         $ar_img = array('gif','png','jpg','jpeg');
         $this->db->where_in('lb_ext',$ar_img);
		 $query = $this->db->get('sys_image_data');
		 return $query->num_rows();
	} 
	
	function upload($data)
	{
        if($this->db->insert('sys_image_data',$data)){
            return true;
        }else{
            return false;
        }
	}
    /**Hieu vo**/
    function get_all_file_dir($dir,$num,$offset,$arr_search=array())
	{
        /*Begin search*/
        if($arr_search)
        {
            switch($arr_search['field_search'])
            {
                case 'TT':
                    $this->db->like('lb_title',$arr_search['key_search']);
                    break ;
                case 'DEC':
                    $this->db->like('description',$arr_search['key_search']);
                    break ;
            }
        }
		/*End search*/
        
         $this->db->where('lb_dir',$dir);
		 $this->db->order_by('image_data_id','DESC');
         
		 $query = $this->db->get('sys_image_data',$num,$offset);
		 //echo $this->db->last_query();
         return $query->result();
	}
	
	function get_num_file_dir($dir,$arr_search=array())
	{
        /*Begin search*/
        if($arr_search)
		{
            switch($arr_search['field_search'])
            {
                case 'TT':
                    $this->db->like('lb_title',$arr_search['key_search']);
                    break ;
                case 'DEC':
                    $this->db->like('description',$arr_search['key_search']);
                    break ;
            }
        }
		/*End search*/
         $this->db->where('lb_dir',$dir);
		 $query = $this->db->get('sys_image_data');
		 return $query->num_rows();
	}
	
    function insert_file($data)
	{
		if($this->db->insert('sys_image_data',$data))
			return true;
		else
			return false;
	}
    function update_file($id,$data){
        if($id>0){
            $this->db->where('image_data_id',$id);
            return $this->db->update('sys_image_data',$data);
        }else{
            return $this->db->insert('sys_image_data',$data);
        }
    }
    function getAllCategoryImage(){
        return $this->db->get('sys_image_category')->result();
    }
    function getFileByID($id){
        $this->db->where('image_data_id',$id);
        return $this->db->get('sys_image_data')->row();
    }
	
    function getFileByName($name)
	{
        $this->db->where('lb_name',$name);
        return $this->db->get('sys_image_data')->row();
    }
	
    function delete($id){
        if($id>0){
            $this->db->where('image_data_id',$id);
            $this->db->delete('sys_image_data');
        }else{
            return false;
        }
    }
}
?>
